const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    baseUrl: app.globalData.baseUrl,
    qrcodeResult: '',
    noticeList: [],
    station: '',
    post: '',
    stationList: [],
    stationIndex: '',
    postList: [],
    postIndex: '',
    qrId: ''
  },

  stationChange: function (e) {
    this.setData({
      stationIndex: e.detail.value,
      station: this.data.stationList[e.detail.value]
    });
    this.getList();
    var that = this;
    wx.request({
      url: (that.data.baseUrl + '/riskNotice/wx/getPost?station=' + that.data.stationList[that.data.stationIndex]).replaceAll('#', '%23'),
      method: 'GET',
      header: {
        'Content-Type': 'application/json' // 请求头  
      },
      success(res) {
        console.log('获取岗位列表:', res.data);
        var data = res.data;
        that.setData({
          postList: data.data,
          postIndex: ''
        })
      }
    });
  },

  stationCancel: function () {
    this.setData({
      station: '',
      post: '',
      stationIndex: '',
      postList: [],
      postIndex: ''
    })
    this.getList();
  },

  postCancel: function () {
    this.setData({
      post: '',
      postIndex: ''
    })
    this.getList();
  },

  postChange: function (e) {
    this.setData({
      postIndex: e.detail.value,
      post: this.data.postList[e.detail.value]
    })
    this.getList();
  },

  scanQrcode: function () {
    var that = this;
    wx.scanCode({
      onlyFromCamera: true,
      success: function (res) {
        that.setData({
          qrcodeResult: res.result
        })
      }
    })
  },

  getList: function () {
    var that = this;
    wx.request({
      url: that.data.baseUrl + '/riskNotice/wx/list?station=' + that.data.station + '&post=' + that.data.post,
      method: 'GET',
      header: {
        'Content-Type': 'application/json',
        'userNo':  wx.getStorageSync('userinfo').siocSapNo // 请求头  
      },
      success(res) {
        console.log('获取风险告知列表:', res.data);
        var data = res.data;
        data.rows.forEach((item,index) => {
          if(1 == item.riskLevel) {
            item.tempColor = 'red';
          }else if(2 == item.riskLevel){
            item.tempColor = 'orange';
          }else if(3 == item.riskLevel){
            item.tempColor = 'yellow';
          }else if(4 == item.riskLevel){
            item.tempColor = 'blue';
          }
        });
        that.setData({
          noticeList: data.rows
        })
      }
    });
  },

  getStation: function () {
    var that = this;
    wx.request({
      url: that.data.baseUrl + '/riskNotice/wx/getStation',
      method: 'GET',
      header: {
        'Content-Type': 'application/json' // 请求头  
      },
      success(res) {
        console.log('获取风险岗位列表:', res.data);
        var data = res.data;
        that.setData({
          stationList: data.data
        })
      }
    });
  },

  initParam: function (qrId) {
    var that = this;
    wx.request({
      url: that.data.baseUrl + '/riskNotice/wx/getParamByQr?id='+qrId,
      method: 'GET',
      header: {
        'Content-Type': 'application/json' // 请求头  
      },
      success(res) {
        console.log('获取二维码信息:', res.data);
        var data = res.data;
        that.setData({
          station: data.data.station,
          post: that.data.post
        });
        that.getListByQr(data.data.station,data.data.post);
      }
    });
  },

  getListByQr: function (station,post) {
    var that = this;
    wx.request({
      url: that.data.baseUrl + '/riskNotice/wx/list?station=' + station + '&post=' + post,
      method: 'GET',
      header: {
        'Content-Type': 'application/json',
        'userNo':  wx.getStorageSync('userinfo').siocSapNo// 请求头  
      },
      success(res) {
        console.log('二维码获取风险告知列表:', res.data);
        var data = res.data;

       
        that.setData({
          noticeList: data.rows
        })
      }
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getStation();
    var qrId = options.id;
    if('undefined' != qrId && null != qrId){
      this.initParam(qrId);
    }else{
      this.getList();
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})